//
//  E7Map.cpp
//  e7engine
//
//  Created by cc on 12-3-7.
//  Copyright (c) 2012年 PalmGame. All rights reserved.
//

#include "E7Map.h"

#include "E7Logic.h"

#include "E7StageData.h"

CE7Map::CE7Map()
{
    m_nID = -1 ;
    m_bIsCreated = false ;
    m_nCreateMapID = -1 ;
    
    m_pCreateCells = NULL ;
    m_pSaveData = NULL ;
    m_nCreateCountrys = 0 ;
    m_nCreateCount = 0 ;
    m_nAllCount = 0 ;

    m_listCells.clear() ;

    m_bIsLoadFlag = false ;
    m_pLoadData = NULL ;
}

CE7Map::~CE7Map()
{
    if (IsCreated())
    {
        Destroy() ;
    }
}

bool CE7Map::Create(int nID, const char* plistfile)
{
    if (IsCreated())
    {
        Destroy() ;
    }

    // todo.

    m_nID = nID ;
    m_bIsCreated = true ;

    return true ;
}

bool CE7Map::Create(int nID, int mapid, int mapsize, int* pData)
{
    if (IsCreated())
    {
        Destroy() ;
    }

    m_nID = nID ;
    m_bIsCreated = true ;
    m_nCreateMapID = mapid ;
    m_pCreateCells = NULL ;
    m_nCreateCountrys = CREATE_MAP_ARRAY[m_nCreateMapID][3] ;
    m_nCreateCount = CREATE_MAP_ARRAY[m_nCreateMapID][2] ;
    m_nAllCount = 0 ;

    m_bIsLoadFlag = true ;
    m_pLoadData = pData ;

    struct timeval ts, te ;
    gettimeofday(&ts, NULL) ;
    LoadMap() ;
    gettimeofday(&te, NULL) ;
    printf("use time : [%lu] micro second\n", (te.tv_sec-ts.tv_sec)*1000000 + (te.tv_usec-ts.tv_usec)) ;

    return true ;
}

bool CE7Map::Create(int nID, const int createid)
{
    if (IsCreated())
    {
        Destroy() ;
    }

    // todo.
    m_nID = nID ;
    m_bIsCreated = true ;
    m_nCreateMapID = createid ;
    m_pCreateCells = NULL ;
    m_nCreateCountrys = CREATE_MAP_ARRAY[m_nCreateMapID][3] ;
    m_nCreateCount = CREATE_MAP_ARRAY[m_nCreateMapID][2] ;
    m_nAllCount = 0 ;

    struct timeval ts, te ;
    gettimeofday(&ts, NULL) ;
    RandomMap() ;
    gettimeofday(&te, NULL) ;
    printf("use time : [%lu] micro second\n", (te.tv_sec-ts.tv_sec)*1000000 + (te.tv_usec-ts.tv_usec)) ;

    return true ;
}

void CE7Map::Destroy()
{
    m_nID = -1 ;
    m_bIsCreated = false ;
    m_nCreateMapID = -1 ;
    SAFE_DELETE_ARR(m_pCreateCells) ;
    SAFE_DELETE_ARR(m_pSaveData) ;
    m_nCreateCountrys = 0 ;
    m_nCreateCount = 0 ;
    m_nAllCount = 0 ;
    m_listCells.clear() ;

    m_bIsLoadFlag = false ;
    SAFE_DELETE_ARR(m_pLoadData) ;
}

void CE7Map::Save(const char* filename)
{
}

void CE7Map::SaveToUserDefault()
{
    CE7StageData::GetStageData()->SaveMapData(m_nID, m_nCreateMapID, "palmgame", m_nAllCount, m_pSaveData) ;
}

void CE7Map::SetSaveMapData()
{
    if (IsLoadMap())
    {
        printf("It is Load Map.\n") ;
        return ;
    }

    for (int i = 0; i < m_nAllCount; ++i)
    {
        if (-1 == m_pCreateCells[i].GetUnitID())
        {
            m_pSaveData[i] = m_pCreateCells[i].GetCountryID() * 100 ;
//            printf("map set cell info - 1: [%d]\n", m_pSaveData[i]) ;
            continue ;
        }
        CE7Unit* pUnit = s_pLogic->GetSelectUnitInfo(m_pCreateCells[i].GetUnitID()) ;
        m_pSaveData[i] = m_pCreateCells[i].GetCountryID() * 100 + pUnit->GetUV() ;
//        printf("map set cell info - 2: [%d]\n", m_pSaveData[i]) ;
    }
}

CE7Cell* CE7Map::GetSelectCellInfo(CCPoint pos)
{
    CE7Cell* pRes = NULL ;

    if (IsPad())
    {
        // reset center positions.
        CCSize s = CCDirector::sharedDirector()->getWinSize() ;
        
        float dist = 0.0f ;
        
        CELL_INFO_LIST::iterator itr ;
        for (itr = m_listCells.begin(); itr != m_listCells.end(); ++itr)
        {
            pRes = (*itr) ;
            dist = sqrt((pRes->GetCenter().x-pos.x)*(pRes->GetCenter().x-pos.x) +
                        (pRes->GetCenter().y-pos.y)*(pRes->GetCenter().y-pos.y)) ;
            if (MAP_CELL_RADIUS >= dist)
            {
                return pRes ;
            }
        }
    }
    else
    {
        // reset center positions.
        CCSize s = CCDirector::sharedDirector()->getWinSize() ;
        
        float dist = 0.0f ;
        
        CELL_INFO_LIST::iterator itr ;
        for (itr = m_listCells.begin(); itr != m_listCells.end(); ++itr)
        {
            pRes = (*itr) ;
            dist = sqrt((pRes->GetCenter().x-pos.x)*(pRes->GetCenter().x-pos.x) +
                        (pRes->GetCenter().y-pos.y)*(pRes->GetCenter().y-pos.y)) ;
            if (MAP_CELL_RADIUS_IPHONE >= dist)
            {
                return pRes ;
            }
        }
    }

//    printf("select cell info flag -- error \n") ;
    return NULL ;
}

CE7Cell* CE7Map::GetSelectCellInfo(int nID)
{
    CELL_INFO_LIST::iterator itr ;
    for (itr = m_listCells.begin(); itr != m_listCells.end(); ++itr)
    {
        if ((*itr)->GetID() == nID)
        {
            return (*itr) ;
        }
    }
    return NULL ;
}

void CE7Map::RandomMap()
{
    CCSize s = CCDirector::sharedDirector()->getWinSize() ;

    if (IsPad())
    {
        float initx = MAP_INIT_X - s.width/2 ;
        float inity = s.height - MAP_INIT_Y ;
        float radius = MAP_CELL_RADIUS ;
        
        int cox = 0 ;
        int coy = 0 ;
        float ofx = radius / 2 ;
        float ofy = -sqrt((radius*radius)-(ofx*ofx)) * 2 ;
        bool isout = false ;
        
        int max_x = CREATE_MAP_ARRAY[m_nCreateMapID][0] ;
        int max_y = CREATE_MAP_ARRAY[m_nCreateMapID][1] ;
        m_nAllCount = max_x * max_y ;
        int i, j ;
        
        //    printf("create info m_nAllCount :[%d]\n", m_nAllCount) ;
        m_pCreateCells = new CE7Cell[m_nAllCount] ;
        m_pSaveData = new int[m_nAllCount] ;
        
        for (i = 0; i < m_nAllCount; ++i)
        {
            if (cox >= max_x)
            {
                cox = 0 ;
                ++coy ;
            }
            
            if (coy >= max_y)
            {
                isout = true ;
                break ;
            }
            
            if (i >= m_nAllCount)
            {
                printf("error ------------ i > m_nAllCount: [%d] [%d]\n", i, m_nAllCount) ;
            }
            m_pCreateCells[i].SetID(i) ;
            m_pCreateCells[i].SetNeighborCount(0) ;
            // 左上[0], 右上[1], 右下[3], 左中[5], 左下[4], 右中[2]. 0-1-2-3-4-5-0
            m_pCreateCells[i].SetBordersPos(0, ccp(initx+cox*radius+cox*ofx+ofx,       inity+coy*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(1, ccp(initx+(cox+1)*radius+cox*ofx+ofx,   inity+coy*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(5, ccp(initx+cox*radius+cox*ofx,           inity+(coy)*ofy+ofy/2+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(2, ccp(initx+(cox+1)*radius+cox*ofx+ofx*2, inity+(coy)*ofy+ofy/2+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(4, ccp(initx+cox*radius+cox*ofx+ofx,       inity+(coy+1)*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(3, ccp(initx+(cox+1)*radius+cox*ofx+ofx,   inity+(coy+1)*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetCenterPos(ccp((m_pCreateCells[i].GetBorders(2).x + m_pCreateCells[i].GetBorders(5).x)/2,
                                               m_pCreateCells[i].GetBorders(2).y)) ;
            
            // set default values.
            m_pCreateCells[i].SetCountryID(-1) ;
            m_pCreateCells[i].SetTownID(-1) ;
            m_pCreateCells[i].SetUnitID(-1) ;
            m_pCreateCells[i].SetUV(UV_NONE) ;
            
            ++cox ;
        }
        
        // get neighbor info.
        CE7Cell* pCell = NULL ;
        CE7Cell* pNeighbor = NULL ;
        for (i = 0; i < m_nAllCount; ++i)
        {
            pCell = &(m_pCreateCells[i]) ;
            for (j = 0; j < m_nAllCount; ++j)
            {
                if (i == j)
                {
                    continue ;
                }
                
                pNeighbor = &(m_pCreateCells[j]) ;
                if ((abs(ofy)+0.01f) >= sqrt((pCell->GetCenter().x-pNeighbor->GetCenter().x)*(pCell->GetCenter().x-pNeighbor->GetCenter().x)+
                                             (pCell->GetCenter().y-pNeighbor->GetCenter().y)*(pCell->GetCenter().y-pNeighbor->GetCenter().y)))
                {
                    if (6 <= pCell->GetNeighborCount())
                    {
                        printf("add neighbors error : [%d] [%d][%d]\n", pCell->GetNeighborCount(), i, j) ;
                        break ;
                    }
                    
                    if (pCell->GetCenter().x-pNeighbor->GetCenter().x > 0.01f &&
                        pCell->GetCenter().y-pNeighbor->GetCenter().y > 0.01f) // left bottom.
                    {
                        pCell->SetNeighborPointers(4, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (pCell->GetCenter().x-pNeighbor->GetCenter().x > 0.01f &&
                             pNeighbor->GetCenter().y-pCell->GetCenter().y > 0.01f) // left top.
                    {
                        pCell->SetNeighborPointers(5, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (abs(pCell->GetCenter().x-pNeighbor->GetCenter().x) <= 0.01f &&
                             pCell->GetCenter().y-pNeighbor->GetCenter().y > 0.01f) // center bottom.
                    {
                        pCell->SetNeighborPointers(3, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (abs(pCell->GetCenter().x-pNeighbor->GetCenter().x) <= 0.01f &&
                             pNeighbor->GetCenter().y-pCell->GetCenter().y > 0.01f) // center top.
                    {
                        pCell->SetNeighborPointers(0, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (pNeighbor->GetCenter().x-pCell->GetCenter().x > 0.01f&&
                             pCell->GetCenter().y-pNeighbor->GetCenter().y > 0.01f ) // right bottom.
                    {
                        pCell->SetNeighborPointers(2, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (pNeighbor->GetCenter().x-pCell->GetCenter().x > 0.01f &&
                             pNeighbor->GetCenter().y-pCell->GetCenter().y > 0.01f) // right top.
                    {
                        pCell->SetNeighborPointers(1, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else
                    {
                        printf("add neighbors error: [%d] [%d]\n", i, j) ;
                    }
                }
            }
        }
        
        // center color.
        m_pCreateCells[m_nAllCount/2].SetCountryID(0) ;
        m_listCells.push_back(&(m_pCreateCells[m_nAllCount/2])) ;
        //    m_pCreateCells[0].SetCountryID(0) ;
        //    m_listCells.push_back(&(m_pCreateCells[0])) ;
        
        // random colors.
        int tempcolor = 1 ;
        //    printf("cell count : [%d]\n", (int)(m_nCreateCountrys * m_nCreateCount)) ;
        
        while (tempcolor < (m_nCreateCountrys * m_nCreateCount))
        {
            i = rand() % m_nAllCount ;
            if (-1 == m_pCreateCells[i].GetCountryID())
            {
                for (j = 0; j < 6; ++j)
                {
                    if (NULL != m_pCreateCells[i].GetNeighborPointer(j))
                    {
                        if (-1 != m_pCreateCells[i].GetNeighborPointer(j)->GetCountryID())
                        {
                            m_pCreateCells[i].SetCountryID(((tempcolor++)%((int)m_nCreateCount+1))%((int)m_nCreateCountrys)) ;
                            //                        m_pCreateCells[i].SetCountryID(1+tempcolor++%1) ;
                            m_listCells.push_back(&(m_pCreateCells[i])) ;
                            break ;
                        }
                    }
                }
            }
        }
        
        m_sMaxSize = CCSizeMake(1024, 768) ;
        for (CELL_INFO_LIST::iterator itr = m_listCells.begin(); itr != m_listCells.end(); ++itr)
        {
            if ((*itr)->GetCountryID() == -1)
            {
                printf("create map error: have countryid = -1 [%d]\n", (*itr)->GetID()) ;
                return ;
            }
            
            if (m_sMaxSize.width < (*itr)->GetCenter().x)
            {
                m_sMaxSize.width = (*itr)->GetCenter().x ; //+ 100 ;
            }
            
            if (m_sMaxSize.height > (*itr)->GetCenter().y)
            {
                m_sMaxSize.height = (*itr)->GetCenter().y ;
            }
        }
        printf("make map info : [%f][%f]\n", m_sMaxSize.width, m_sMaxSize.height) ;
        m_sMaxSize.width += 100 ;
        m_sMaxSize.height = 768 - m_sMaxSize.height +100 ;
    }
    else // iphone.
    {
        float initx = MAP_INIT_X_IPHONE - s.width/2 ;
        float inity = s.height - MAP_INIT_Y_IPHONE ;
        float radius = MAP_CELL_RADIUS_IPHONE ;
        
        int cox = 0 ;
        int coy = 0 ;
        float ofx = radius / 2 ;
        float ofy = -sqrt((radius*radius)-(ofx*ofx)) * 2 ;
        bool isout = false ;
        
        int max_x = CREATE_MAP_ARRAY[m_nCreateMapID][0] ;
        int max_y = CREATE_MAP_ARRAY[m_nCreateMapID][1] ;
        m_nAllCount = max_x * max_y ;
        int i, j ;
        
        //    printf("create info m_nAllCount :[%d]\n", m_nAllCount) ;
        m_pCreateCells = new CE7Cell[m_nAllCount] ;
        m_pSaveData = new int[m_nAllCount] ;
        
        for (i = 0; i < m_nAllCount; ++i)
        {
            if (cox >= max_x)
            {
                cox = 0 ;
                ++coy ;
            }
            
            if (coy >= max_y)
            {
                isout = true ;
                break ;
            }
            
            if (i >= m_nAllCount)
            {
                printf("error ------------ i > m_nAllCount: [%d] [%d]\n", i, m_nAllCount) ;
            }
            m_pCreateCells[i].SetID(i) ;
            m_pCreateCells[i].SetNeighborCount(0) ;
            // 左上[0], 右上[1], 右下[3], 左中[5], 左下[4], 右中[2]. 0-1-2-3-4-5-0
            m_pCreateCells[i].SetBordersPos(0, ccp(initx+cox*radius+cox*ofx+ofx,       inity+coy*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(1, ccp(initx+(cox+1)*radius+cox*ofx+ofx,   inity+coy*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(5, ccp(initx+cox*radius+cox*ofx,           inity+(coy)*ofy+ofy/2+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(2, ccp(initx+(cox+1)*radius+cox*ofx+ofx*2, inity+(coy)*ofy+ofy/2+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(4, ccp(initx+cox*radius+cox*ofx+ofx,       inity+(coy+1)*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(3, ccp(initx+(cox+1)*radius+cox*ofx+ofx,   inity+(coy+1)*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetCenterPos(ccp((m_pCreateCells[i].GetBorders(2).x + m_pCreateCells[i].GetBorders(5).x)/2,
                                               m_pCreateCells[i].GetBorders(2).y)) ;
            
            // set default values.
            m_pCreateCells[i].SetCountryID(-1) ;
            m_pCreateCells[i].SetTownID(-1) ;
            m_pCreateCells[i].SetUnitID(-1) ;
            m_pCreateCells[i].SetUV(UV_NONE) ;
            
            ++cox ;
        }
        
        // get neighbor info.
        CE7Cell* pCell = NULL ;
        CE7Cell* pNeighbor = NULL ;
        for (i = 0; i < m_nAllCount; ++i)
        {
            pCell = &(m_pCreateCells[i]) ;
            for (j = 0; j < m_nAllCount; ++j)
            {
                if (i == j)
                {
                    continue ;
                }
                
                pNeighbor = &(m_pCreateCells[j]) ;
                if ((abs(ofy)+0.01f) >= sqrt((pCell->GetCenter().x-pNeighbor->GetCenter().x)*(pCell->GetCenter().x-pNeighbor->GetCenter().x)+
                                             (pCell->GetCenter().y-pNeighbor->GetCenter().y)*(pCell->GetCenter().y-pNeighbor->GetCenter().y)))
                {
                    if (6 <= pCell->GetNeighborCount())
                    {
                        printf("add neighbors error : [%d] [%d][%d]\n", pCell->GetNeighborCount(), i, j) ;
                        break ;
                    }
                    
                    if (pCell->GetCenter().x-pNeighbor->GetCenter().x > 0.01f &&
                        pCell->GetCenter().y-pNeighbor->GetCenter().y > 0.01f) // left bottom.
                    {
                        pCell->SetNeighborPointers(4, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (pCell->GetCenter().x-pNeighbor->GetCenter().x > 0.01f &&
                             pNeighbor->GetCenter().y-pCell->GetCenter().y > 0.01f) // left top.
                    {
                        pCell->SetNeighborPointers(5, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (abs(pCell->GetCenter().x-pNeighbor->GetCenter().x) <= 0.01f &&
                             pCell->GetCenter().y-pNeighbor->GetCenter().y > 0.01f) // center bottom.
                    {
                        pCell->SetNeighborPointers(3, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (abs(pCell->GetCenter().x-pNeighbor->GetCenter().x) <= 0.01f &&
                             pNeighbor->GetCenter().y-pCell->GetCenter().y > 0.01f) // center top.
                    {
                        pCell->SetNeighborPointers(0, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (pNeighbor->GetCenter().x-pCell->GetCenter().x > 0.01f&&
                             pCell->GetCenter().y-pNeighbor->GetCenter().y > 0.01f ) // right bottom.
                    {
                        pCell->SetNeighborPointers(2, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (pNeighbor->GetCenter().x-pCell->GetCenter().x > 0.01f &&
                             pNeighbor->GetCenter().y-pCell->GetCenter().y > 0.01f) // right top.
                    {
                        pCell->SetNeighborPointers(1, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else
                    {
                        printf("add neighbors error: [%d] [%d]\n", i, j) ;
                    }
                }
            }
        }
        
        // center color.
        m_pCreateCells[m_nAllCount/2].SetCountryID(0) ;
        m_listCells.push_back(&(m_pCreateCells[m_nAllCount/2])) ;
        //    m_pCreateCells[0].SetCountryID(0) ;
        //    m_listCells.push_back(&(m_pCreateCells[0])) ;
        
        // random colors.
        int tempcolor = 1 ;
        //    printf("cell count : [%d]\n", (int)(m_nCreateCountrys * m_nCreateCount)) ;
        
        while (tempcolor < (m_nCreateCountrys * m_nCreateCount))
        {
            i = rand() % m_nAllCount ;
            if (-1 == m_pCreateCells[i].GetCountryID())
            {
                for (j = 0; j < 6; ++j)
                {
                    if (NULL != m_pCreateCells[i].GetNeighborPointer(j))
                    {
                        if (-1 != m_pCreateCells[i].GetNeighborPointer(j)->GetCountryID())
                        {
                            m_pCreateCells[i].SetCountryID(((tempcolor++)%((int)m_nCreateCount+1))%((int)m_nCreateCountrys)) ;
                            //                        m_pCreateCells[i].SetCountryID(1+tempcolor++%1) ;
                            m_listCells.push_back(&(m_pCreateCells[i])) ;
                            break ;
                        }
                    }
                }
            }
        }
        
        m_sMaxSize = CCSizeMake(480, 320) ;
        for (CELL_INFO_LIST::iterator itr = m_listCells.begin(); itr != m_listCells.end(); ++itr)
        {
            if ((*itr)->GetCountryID() == -1)
            {
                printf("create map error: have countryid = -1 [%d]\n", (*itr)->GetID()) ;
                return ;
            }
            
            if (m_sMaxSize.width < (*itr)->GetCenter().x)
            {
                m_sMaxSize.width = (*itr)->GetCenter().x ; //+ 100 ;
            }
            
            if (m_sMaxSize.height > (*itr)->GetCenter().y)
            {
                m_sMaxSize.height = (*itr)->GetCenter().y ;
            }
        }
        printf("make map info : [%f][%f]\n", m_sMaxSize.width, m_sMaxSize.height) ;
        m_sMaxSize.width += 50 ;
        m_sMaxSize.height = 320 - m_sMaxSize.height + 50 ;
    }
}

void CE7Map::LoadMap()
{
    CCSize s = CCDirector::sharedDirector()->getWinSize() ;

    if (IsPad())
    {
        float initx = MAP_INIT_X - s.width/2 ;
        float inity = s.height - MAP_INIT_Y ;
        float radius = MAP_CELL_RADIUS ;
        
        int cox = 0 ;
        int coy = 0 ;
        float ofx = radius / 2 ;
        float ofy = -sqrt((radius*radius)-(ofx*ofx)) * 2 ;
        bool isout = false ;
        
        int max_x = CREATE_MAP_ARRAY[m_nCreateMapID][0] ;
        int max_y = CREATE_MAP_ARRAY[m_nCreateMapID][1] ;
        m_nAllCount = max_x * max_y ;
        int i, j ;
        
        //    printf("create info m_nAllCount :[%d]\n", m_nAllCount) ;
        m_pCreateCells = new CE7Cell[m_nAllCount] ;
        
        for (i = 0; i < m_nAllCount; ++i)
        {
            if (cox >= max_x)
            {
                cox = 0 ;
                ++coy ;
            }
            
            if (coy >= max_y)
            {
                isout = true ;
                break ;
            }
            
            if (i >= m_nAllCount)
            {
                printf("error ------------ i > m_nAllCount: [%d] [%d]\n", i, m_nAllCount) ;
            }
            m_pCreateCells[i].SetID(i) ;
            m_pCreateCells[i].SetNeighborCount(0) ;
            // 左上[0], 右上[1], 右下[3], 左中[5], 左下[4], 右中[2]. 0-1-2-3-4-5-0
            m_pCreateCells[i].SetBordersPos(0, ccp(initx+cox*radius+cox*ofx+ofx,       inity+coy*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(1, ccp(initx+(cox+1)*radius+cox*ofx+ofx,   inity+coy*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(5, ccp(initx+cox*radius+cox*ofx,           inity+(coy)*ofy+ofy/2+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(2, ccp(initx+(cox+1)*radius+cox*ofx+ofx*2, inity+(coy)*ofy+ofy/2+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(4, ccp(initx+cox*radius+cox*ofx+ofx,       inity+(coy+1)*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(3, ccp(initx+(cox+1)*radius+cox*ofx+ofx,   inity+(coy+1)*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetCenterPos(ccp((m_pCreateCells[i].GetBorders(2).x + m_pCreateCells[i].GetBorders(5).x)/2,
                                               m_pCreateCells[i].GetBorders(2).y)) ;
            
            // set default values.
            //        printf("map create info: cell id: [%d]\n", (int)(m_pLoadData[i]/100)) ;
            m_pCreateCells[i].SetCountryID((int)(m_pLoadData[i]/100)) ;
            m_pCreateCells[i].SetTownID(-1) ;
            m_pCreateCells[i].SetUnitID(-1) ;
            m_pCreateCells[i].SetUV(UV_NONE) ; //flag.
            
            if (-1 != m_pCreateCells[i].GetCountryID())
            {
                m_listCells.push_back(&m_pCreateCells[i]) ;
            }
            
            ++cox ;
        }
        
        // get neighbor info.
        CE7Cell* pCell = NULL ;
        CE7Cell* pNeighbor = NULL ;
        for (i = 0; i < m_nAllCount; ++i)
        {
            pCell = &(m_pCreateCells[i]) ;
            for (j = 0; j < m_nAllCount; ++j)
            {
                if (i == j)
                {
                    continue ;
                }
                
                pNeighbor = &(m_pCreateCells[j]) ;
                if ((abs(ofy)+0.01f) >= sqrt((pCell->GetCenter().x-pNeighbor->GetCenter().x)*(pCell->GetCenter().x-pNeighbor->GetCenter().x)+
                                             (pCell->GetCenter().y-pNeighbor->GetCenter().y)*(pCell->GetCenter().y-pNeighbor->GetCenter().y)))
                {
                    if (6 <= pCell->GetNeighborCount())
                    {
                        printf("add neighbors error : [%d] [%d][%d]\n", pCell->GetNeighborCount(), i, j) ;
                        break ;
                    }
                    
                    if (pCell->GetCenter().x-pNeighbor->GetCenter().x > 0.01f &&
                        pCell->GetCenter().y-pNeighbor->GetCenter().y > 0.01f) // left bottom.
                    {
                        pCell->SetNeighborPointers(4, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (pCell->GetCenter().x-pNeighbor->GetCenter().x > 0.01f &&
                             pNeighbor->GetCenter().y-pCell->GetCenter().y > 0.01f) // left top.
                    {
                        pCell->SetNeighborPointers(5, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (abs(pCell->GetCenter().x-pNeighbor->GetCenter().x) <= 0.01f &&
                             pCell->GetCenter().y-pNeighbor->GetCenter().y > 0.01f) // center bottom.
                    {
                        pCell->SetNeighborPointers(3, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (abs(pCell->GetCenter().x-pNeighbor->GetCenter().x) <= 0.01f &&
                             pNeighbor->GetCenter().y-pCell->GetCenter().y > 0.01f) // center top.
                    {
                        pCell->SetNeighborPointers(0, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (pNeighbor->GetCenter().x-pCell->GetCenter().x > 0.01f&&
                             pCell->GetCenter().y-pNeighbor->GetCenter().y > 0.01f ) // right bottom.
                    {
                        pCell->SetNeighborPointers(2, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (pNeighbor->GetCenter().x-pCell->GetCenter().x > 0.01f &&
                             pNeighbor->GetCenter().y-pCell->GetCenter().y > 0.01f) // right top.
                    {
                        pCell->SetNeighborPointers(1, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else
                    {
                        printf("add neighbors error: [%d] [%d]\n", i, j) ;
                    }
                }
            }
        }
        
        m_sMaxSize = CCSizeMake(1024, 768) ;
        for (CELL_INFO_LIST::iterator itr = m_listCells.begin(); itr != m_listCells.end(); ++itr)
        {
            if ((*itr)->GetCountryID() == -1)
            {
                printf("load map error: have countryid = -1 [%d]\n", (*itr)->GetID()) ;
                return ;
            }
            
            if (m_sMaxSize.width < (*itr)->GetCenter().x)
            {
                m_sMaxSize.width = (*itr)->GetCenter().x ; // + 100 ;
            }
            
            if (m_sMaxSize.height > (*itr)->GetCenter().y)
            {
                m_sMaxSize.height = (*itr)->GetCenter().y ;
            }
        }
        printf("load map info : [%f][%f]\n", m_sMaxSize.width, m_sMaxSize.height) ;
        m_sMaxSize.width += 100 ;
        m_sMaxSize.height = 768 - m_sMaxSize.height + 100 ;
    }
    else // iphone.
    {
        float initx = MAP_INIT_X_IPHONE - s.width/2 ;
        float inity = s.height - MAP_INIT_Y_IPHONE ;
        float radius = MAP_CELL_RADIUS_IPHONE ;
        
        int cox = 0 ;
        int coy = 0 ;
        float ofx = radius / 2 ;
        float ofy = -sqrt((radius*radius)-(ofx*ofx)) * 2 ;
        bool isout = false ;
        
        int max_x = CREATE_MAP_ARRAY[m_nCreateMapID][0] ;
        int max_y = CREATE_MAP_ARRAY[m_nCreateMapID][1] ;
        m_nAllCount = max_x * max_y ;
        int i, j ;
        
        //    printf("create info m_nAllCount :[%d]\n", m_nAllCount) ;
        m_pCreateCells = new CE7Cell[m_nAllCount] ;
        
        for (i = 0; i < m_nAllCount; ++i)
        {
            if (cox >= max_x)
            {
                cox = 0 ;
                ++coy ;
            }
            
            if (coy >= max_y)
            {
                isout = true ;
                break ;
            }
            
            if (i >= m_nAllCount)
            {
                printf("error ------------ i > m_nAllCount: [%d] [%d]\n", i, m_nAllCount) ;
            }
            m_pCreateCells[i].SetID(i) ;
            m_pCreateCells[i].SetNeighborCount(0) ;
            // 左上[0], 右上[1], 右下[3], 左中[5], 左下[4], 右中[2]. 0-1-2-3-4-5-0
            m_pCreateCells[i].SetBordersPos(0, ccp(initx+cox*radius+cox*ofx+ofx,       inity+coy*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(1, ccp(initx+(cox+1)*radius+cox*ofx+ofx,   inity+coy*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(5, ccp(initx+cox*radius+cox*ofx,           inity+(coy)*ofy+ofy/2+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(2, ccp(initx+(cox+1)*radius+cox*ofx+ofx*2, inity+(coy)*ofy+ofy/2+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(4, ccp(initx+cox*radius+cox*ofx+ofx,       inity+(coy+1)*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetBordersPos(3, ccp(initx+(cox+1)*radius+cox*ofx+ofx,   inity+(coy+1)*ofy+(cox%2)*ofy/2)) ;
            m_pCreateCells[i].SetCenterPos(ccp((m_pCreateCells[i].GetBorders(2).x + m_pCreateCells[i].GetBorders(5).x)/2,
                                               m_pCreateCells[i].GetBorders(2).y)) ;
            
            // set default values.
            //        printf("map create info: cell id: [%d]\n", (int)(m_pLoadData[i]/100)) ;
            m_pCreateCells[i].SetCountryID((int)(m_pLoadData[i]/100)) ;
            m_pCreateCells[i].SetTownID(-1) ;
            m_pCreateCells[i].SetUnitID(-1) ;
            m_pCreateCells[i].SetUV(UV_NONE) ; //flag.
            
            if (-1 != m_pCreateCells[i].GetCountryID())
            {
                m_listCells.push_back(&m_pCreateCells[i]) ;
            }
            
            ++cox ;
        }
        
        // get neighbor info.
        CE7Cell* pCell = NULL ;
        CE7Cell* pNeighbor = NULL ;
        for (i = 0; i < m_nAllCount; ++i)
        {
            pCell = &(m_pCreateCells[i]) ;
            for (j = 0; j < m_nAllCount; ++j)
            {
                if (i == j)
                {
                    continue ;
                }
                
                pNeighbor = &(m_pCreateCells[j]) ;
                if ((abs(ofy)+0.01f) >= sqrt((pCell->GetCenter().x-pNeighbor->GetCenter().x)*(pCell->GetCenter().x-pNeighbor->GetCenter().x)+
                                             (pCell->GetCenter().y-pNeighbor->GetCenter().y)*(pCell->GetCenter().y-pNeighbor->GetCenter().y)))
                {
                    if (6 <= pCell->GetNeighborCount())
                    {
                        printf("add neighbors error : [%d] [%d][%d]\n", pCell->GetNeighborCount(), i, j) ;
                        break ;
                    }
                    
                    if (pCell->GetCenter().x-pNeighbor->GetCenter().x > 0.01f &&
                        pCell->GetCenter().y-pNeighbor->GetCenter().y > 0.01f) // left bottom.
                    {
                        pCell->SetNeighborPointers(4, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (pCell->GetCenter().x-pNeighbor->GetCenter().x > 0.01f &&
                             pNeighbor->GetCenter().y-pCell->GetCenter().y > 0.01f) // left top.
                    {
                        pCell->SetNeighborPointers(5, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (abs(pCell->GetCenter().x-pNeighbor->GetCenter().x) <= 0.01f &&
                             pCell->GetCenter().y-pNeighbor->GetCenter().y > 0.01f) // center bottom.
                    {
                        pCell->SetNeighborPointers(3, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (abs(pCell->GetCenter().x-pNeighbor->GetCenter().x) <= 0.01f &&
                             pNeighbor->GetCenter().y-pCell->GetCenter().y > 0.01f) // center top.
                    {
                        pCell->SetNeighborPointers(0, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (pNeighbor->GetCenter().x-pCell->GetCenter().x > 0.01f&&
                             pCell->GetCenter().y-pNeighbor->GetCenter().y > 0.01f ) // right bottom.
                    {
                        pCell->SetNeighborPointers(2, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else if (pNeighbor->GetCenter().x-pCell->GetCenter().x > 0.01f &&
                             pNeighbor->GetCenter().y-pCell->GetCenter().y > 0.01f) // right top.
                    {
                        pCell->SetNeighborPointers(1, pNeighbor) ;
                        pCell->SetNeighborCount(pCell->GetNeighborCount()+1) ;
                    }
                    else
                    {
                        printf("add neighbors error: [%d] [%d]\n", i, j) ;
                    }
                }
            }
        }
        
        m_sMaxSize = CCSizeMake(480, 320) ;
        for (CELL_INFO_LIST::iterator itr = m_listCells.begin(); itr != m_listCells.end(); ++itr)
        {
            if ((*itr)->GetCountryID() == -1)
            {
                printf("load map error: have countryid = -1 [%d]\n", (*itr)->GetID()) ;
                return ;
            }
            
            if (m_sMaxSize.width < (*itr)->GetCenter().x)
            {
                m_sMaxSize.width = (*itr)->GetCenter().x ; // + 100 ;
            }
            
            if (m_sMaxSize.height > (*itr)->GetCenter().y)
            {
                m_sMaxSize.height = (*itr)->GetCenter().y ;
            }
        }
        printf("load map info : [%f][%f]\n", m_sMaxSize.width, m_sMaxSize.height) ;
        m_sMaxSize.width += 50 ;
        m_sMaxSize.height = 320 - m_sMaxSize.height + 50 ;
    }
}


















